import Vue from 'vue'
import utils from './utils'
import {request} from "./httpSever"
import {sso_login} from "@/plugins/sso-login";

export default function ({app, $axios, store, route, redirect}) {
  Vue.config.productionTip = false;
  Vue.prototype.utils = utils;
  Vue.prototype.$request = request;
  Vue.prototype.showSubscriber = function () {
    if (localStorage.getItem('token')) {
      this.$request.verificationToken().then(res => {
        if (res.status == 200) {
          // console.log(res.data);
          if (res.data) {
            store.commit('changeSubscriberShow', true);
          } else {
            sso_login()
          }
        } else {
          this.requestError(res.status, res.message);
        }
      }, err => {
        console.log(err);
        this.requestFail(err);
      })
    } else {
      sso_login()
    }
  };
  Vue.prototype.requestError = function (status, msg = '') {//状态码异常的处理
    if (status == 401 || status == 403) {
      sso_login()
    } else {
      if (msg) {
        this.$message({
          type: 'warning',
          message: msg
        });
      } else {
        this.$message({
          type: 'warning',
          message: '请求错误，请稍后重试'
        });
      }

    }
  };

  Vue.prototype.requestFail = function (errMsg) {//请求失败的处理
    this.$message({
      type: 'warning',
      message: errMsg ? errMsg : '请求错误，请稍后重试！'
    });
  };
  Vue.prototype.currentUser = function () {
    if (localStorage.getItem('token')) {
      this.$request.currentUser().then((res) => {
        console.log("-----首页用户权限-------", res)
        if (res.status == 200) {
          let resData = res.data;
          store.commit('changeNickName', resData.nickname);
          store.commit('changeTel', resData.tel);
          if (resData.roles.some((item) => item.id == 1 || item.id == 2)) {//后台管理员
            console.log('后台管理员')
          } else {
            console.log('客户端用户')
          }
        }

      }, (err) => {
        console.log(err);
        this.requestFail(err);
      })
    }
  };
}

//控制是否开启 console.log
/*let logDebug = false;
if (process.env.NODE_ENV === 'development') {
    logDebug = true;
} else if (process.env.NODE_ENV === 'production') {
    logDebug = false;
}
console.log = (function (oriLogFunc) {
    return function () {
        if (logDebug) {
            oriLogFunc.apply(this, arguments);
        }
    }
})(console.log);*/
